<?php

namespace App\Admin\Table;

use App\Admin\Repositories\UserManagement;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UserManagementTable extends AdminController
{
    private $id;

    /**
     * 表格展示
     * @return Grid
     */
    public function grid(): Grid
    {
        return Grid::make(new UserManagement(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('email');
            $grid->column('state')->display(function ($state) {
                $class = ['text-warning', 'text-success', 'text-danger'][$state];
                $_state = ['未登录', '登录成功', '登录失败'][$state];
                return "<i class='fa fa-circle $class' style='width: 10px; height: 10px'></i>&emsp;{$_state}";
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->showCreateButton();
            $userManagementTable = new UserManagementTable();
            $userManagementTable->filter($grid);
        });
    }

    /**
     * 筛选数据
     * @param Grid $grid
     * @return void
     */
    private function filter(Grid $grid): void
    {
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->equal('name');
        });
    }

    /**
     * 新增表单
     * @return Form
     */
    public function addForm(): Form
    {
        return Form::make(new UserManagement(), function (Form $form) {
            $this->extracted($form);
        });
    }

    /**
     * 查看
     * @param $id
     * @return Show
     */
    public function detail($id): Show
    {
        return Show::make($id, new UserManagement(), static function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('email');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    public function editForm($id): Form
    {
        $this->id = $id;
        return Form::make(new UserManagement(), function (Form $form) {
            $form->edit($this->id);
//            设置表单的title
            $form->title(admin_trans('admin.edit'));
            $this->extracted($form);
        });
    }

    /**
     * @param Form $form
     * @return void
     */
    public function extracted(Form $form): void
    {
        $form->display('id');
        $form->text('name');
        $form->email('email');
        $form->text('password');
        $form->display('created_at');
        $form->display('updated_at');
//            去掉'查看'复选框
        $form->disableViewCheck();
//            去掉'继续编辑'复选框
        $form->disableEditingCheck();
//            去掉'继续创建'复选框
        $form->disableCreatingCheck();
    }
}
